package cn.fbs.service;

import cn.fbs.dao.dm.DMQbCategoryConnectionMapper;
import cn.fbs.dao.dm.SysUserMapper;
import cn.fbs.model.TQbCategoryConnection;
import cn.fbs.model.dm.SysUser;
import cn.hutool.core.map.MapUtil;
import cn.fbs.dao.dm.DMCodeCategoryTypeMapper;
import cn.fbs.dao.dm.DMLabelGroupInfoMapper;
import cn.fbs.model.TCodeCategoryType;
import cn.fbs.model.TLabelGroupInfo;
import cn.fbs.util.ChineseCharacterUtil;
import cn.fbs.vo.CateAndGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CategoryService {
    @Autowired
    private DMCodeCategoryTypeMapper mapper;
    @Autowired
    private SysUserMapper userInfoMapper;
    @Autowired
    private DMLabelGroupInfoMapper groupMapper;
    @Autowired
    private DMQbCategoryConnectionMapper connectionMapper;
    public List<TCodeCategoryType> findAll() {
        List<TCodeCategoryType> list = mapper.selectAll();
        return list;
    }

    public String insert(TCodeCategoryType targetType) {

        targetType.setCreateDate(new Date());

        String categoryName = targetType.getCategoryName();

        /**
         * 判断标签组是否存在
         */
        TLabelGroupInfo labelGroupInfo = groupMapper.selectByPrimaryKey(targetType.getGroupId());
        if (labelGroupInfo == null) {
            return "500,请先保存标签组";
        }
        int count = mapper.selectCountByIdAndPid(categoryName, targetType.getGroupId());
        if (count == 0) {
            mapper.insertSelective(targetType);
            return "200,新增标签成功";
        } else {
            return "500,同一标签组下不能新增同名标签";
        }

    }

    public String update(TCodeCategoryType targetType) {
        targetType.setUpdateDate(new Date());
        String categoryName = targetType.getCategoryName();

        int count = mapper.selectCountByIdAndPid(categoryName, targetType.getGroupId());
        if (count == 0) {
            mapper.updateByPrimaryKeySelective(targetType);
            return "200,修改标签成功";
        } else {
            return "500,同一标签组下不能存在同名标签";
        }
    }

    public String delete(Integer primaryKey) {
        mapper.deleteByPrimaryKey(primaryKey);
        return "200,修改标签成功";
    }

    public TCodeCategoryType selectByPrimaryKey(Integer primaryKey) {
        return mapper.selectByPrimaryKey(primaryKey);
    }

    public Map<String, List<TCodeCategoryType>> findAllGroupByCateGory() {
        HashMap<String, List<TCodeCategoryType>> resultMap = new HashMap<>();
        List<TCodeCategoryType> list = mapper.selectAll();
        for (TCodeCategoryType tCodeCategoryType : list) {
            String firstLetter = ChineseCharacterUtil.convertHanzi2Pinyin(tCodeCategoryType.getCategoryName(), false).toUpperCase().substring(0, 1);
            tCodeCategoryType.setFirstLetter(firstLetter);
            if (resultMap.containsKey(firstLetter)) {
                List<TCodeCategoryType> subList = resultMap.get(firstLetter);
                subList.add(tCodeCategoryType);
            } else {
                ArrayList<TCodeCategoryType> subList = new ArrayList<>();
                subList.add(tCodeCategoryType);
                resultMap.put(firstLetter, subList);
            }
        }
        TreeMap<String, List<TCodeCategoryType>> sortResultMap = MapUtil.sort(resultMap);
        return sortResultMap;
    }

    public List<TCodeCategoryType> likeQuery(String categoryName) {
        return mapper.likeQuery(categoryName);
    }

    public List<TCodeCategoryType> findByGroupIdQuery(String groupId) {
        List<TCodeCategoryType> list = mapper.findByGroupIdQuery(groupId);
        return list;
    }

    public List<CateAndGroupVo> categoryAndGroup() {
        List<CateAndGroupVo> cateAndGroupVos = groupMapper.categoryAndGroup();
        ArrayList<CateAndGroupVo> result = new ArrayList<>();
        for (CateAndGroupVo cateAndGroupVo : cateAndGroupVos) {
            if (cateAndGroupVo.getCategoryTypeList() != null && cateAndGroupVo.getCategoryTypeList().size() != 0) {
                result.add(cateAndGroupVo);
            }
        }
        return result;
    }

    public List<CateAndGroupVo> allCategoryAndGroup() {
        return groupMapper.categoryAndGroup();


    }

    public List<CateAndGroupVo> inherentLabel() {

        return groupMapper.inherentLabel();

    }
    public void insertCategory(List<Long> categoryIds, String operator, Long id, Integer connectionType) {
        if (categoryIds == null) {
            return;
        }
        ArrayList<TQbCategoryConnection> list = new ArrayList<>();
        HashMap<String, SysUser> map = new HashMap<>();
        for (Long categoryId : categoryIds) {
            TQbCategoryConnection categoryConnection = new TQbCategoryConnection();
            categoryConnection.setCategoryId(categoryId);
            categoryConnection.setMessageId(id);
            SysUser userInfo;
            if (!map.containsKey(operator)) {
                userInfo = userInfoMapper.selectByUserName(operator);
                map.put(operator, userInfo);
            } else {
                userInfo = map.get(operator);
            }
            if (userInfo == null) {
                return;
            }
            categoryConnection.setUserId(userInfo.getUserId());
            categoryConnection.setConnectionType(connectionType);
            list.add(categoryConnection);
//            connectionMapper.insert(categoryConnection);
        }
        connectionMapper.batchInsert(list);
    }


}
